package system_storage_service

import (
	"time"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
)

func (cls *Storage) GetObjectAccessedUrl(ctx context.Context, object *model.SystemObject, timeout time.Duration) (string, errors.Error) {
	if nil == object {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISS_GE.S_GE.GOAU_RL.143008 object is nil")

		return "", errors.ErrorServerInternalError("ISS_GE.S_GE.GOAU_RL.145350", "异常的传入参数")
	}

	if model.SystemObjectStatusUploading == *object.Status {
		return "", errors.New(errors.ErrorTypeInvalidRequest, "ISS_GE.S_GE.GOAU_RL.185404", "对象上传中，无法获取访问URL")
	}

	if model.SystemObjectStatusFailed == *object.Status {
		return "", errors.New(errors.ErrorTypeInvalidRequest, "ISS_GE.S_GE.GOAU_RL.225417", "对象上传失败，无法获取访问URL")
	}

	provider, err := cls.GetProviderByName(*object.Provider)
	if err != nil {
		return "", err.Relation(errors.ErrorServerInternalError("ISS_GE.S_GE.GOAU_RL.275432", "获取存储提供商失败"))
	}

	url, err := provider.GetObjectAccessedUrl(ctx, *object.Filepath, timeout)
	if err != nil {
		return "", err.Relation(errors.ErrorServerInternalError("ISS_GE.S_GE.GOAU_RL.325523", "获取对象访问URL失败"))
	}

	return url, nil
}
